<?php
/*
    * 类与对象 访问控制(可见性)
    * https://www.php.net/manual/zh/language.oop5.decon.php
    * Adair  zouhdai@gmail.com
    * 2021-08-17
*/

class Test
{
    private $foo;

    public function __construct($foo)
    {
        $this->foo = $foo;
        echo 'aaa--- : '.$this->foo."<br />";
    }

    private function bar()
    {
        echo 'Accessed the private method.';
    }

    public function baz(Test $other)
    {
        // We can change the private property:
        $other->foo = 'hello';
        var_dump($other->foo.' --- '.$this->foo);

        // We can also call the private method:
        $other->bar();
    }
}

$test = new Test('test');

$test->baz(new Test('other'));







class test1 {
    public $public = 'Public var';
    protected $protected = 'protected var';
    private $private = 'Private var';
   
    public static $static_public = 'Public static var';
    protected static $static_protected = 'protected static var';
    private static $static_private = 'Private static var';
}

$class = new test1;
print_r($class);
print_r(test1::$static_public);
print_r($class::$static_public);
echo "<br />";



class ParentClass {

    public function execute($method) {
        $this->$method();
    }

    private function privateMethod() {
        echo "hi, i'm private"."<br />";
    }
   
}

class ChildClass extends ParentClass {

    
   
    protected function protectedMethod() {
        echo "hi, i'm protected"."<br />";
    }

    private function privateMethod() {
        echo "hi, i'm private"."<br />";
    }
   
}

$object = new ChildClass();

$object->execute('protectedMethod');

$object->execute('privateMethod');






echo "PHP Version: ".phpversion()."<br />";

class Foo
{
   private   $bar  = "value of private var";
   protected $bar2 = "value of protected var";
   public    $bar3 = "value of public var";
}

$obj = new Foo;
var_export($obj);
echo serialize($obj) ."<br />";

print_r($obj);
echo "<br /><br /><br />";
print_r((array)$obj);
$arr = (array)$obj;
$newarr = [];
foreach($arr as $key =>$val){
    $newarr[] = $val;
}
var_dump($newarr[0],$newarr[1],$newarr[2]);
echo "<br /><br /><br />";
echo ($obj->bar3) ."<br />";
/*echo ($obj->bar2) ."<br />";
echo ($obj->bar) ."<br />";*/








class vessel{
    private $things = array();
    
    public function setThing($things){
            $this->things = $things;
    }

    public function getThing($obj){
        return $obj->things;
    }
}

class smallVessel extends vessel{
    private $things = array();
    
    public function setThing($things){
        $this->things = $things;
    }

    public function getThing1($obj){
        return $obj->things;
    }
}

$basket = new vessel();
$bucket = new vessel();
$bowl = new smallVessel();

$basket->setThing(array('wine' , 'water' , 'sugar'));

// returns the contents inside basket unexpectedly
print_r($bucket->getThing($basket));

// returns error, quite rightly so!
print_r($bowl->getThing($basket));
